package org.shs.company.dao;

import static org.shs.common.config.SystemInitializer.sqlHandler;

import java.util.ArrayList;

import org.shs.common.utils.DataHolder;
import org.shs.company.form.CompanyForm;
import org.shs.company.model.CompanyModel;
import org.shs.worker.model.WorkerModel;

/**
 * Implementation of Database related operations for the Company module. This
 * implementation fetches the data from a MySQL backend.
 * <br/>
 * <br/><b>Created:</b>&nbsp;&nbsp; Dec 23, 2008 1:38:59 AM
 *
 * @author Gunjan
 *
 */
public class CompanyDAOImpl implements ICompanyDAO
{

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#getCompanies(
    *    org.shs.common.utils.DataHolder)
    */
   @SuppressWarnings("unchecked")
   public ArrayList<CompanyModel> getCompanies(DataHolder cmpnyData)
            throws Exception
   {
      return (ArrayList<CompanyModel>)sqlHandler.queryForList(
                                          "Company.getCompanies", cmpnyData);
   }

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#getCompanyDetails(java.lang.String)
    */
   public CompanyModel getCompanyDetails(String companyId) throws Exception
   {
      return (CompanyModel) sqlHandler.queryForObject(
                              "Company.getCompanyDetails", companyId);
   }

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#addNewCompanyData(org.shs.company.form.CompanyForm)
    */
   public String addNewCompanyData(CompanyForm companyData) throws Exception
   {
      return (String) sqlHandler.insert("Company.addNewCompanyData", companyData);
   }

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#editCompanyData(
    *    org.shs.company.form.CompanyForm)
    */
   public void editCompanyData(CompanyForm companyData) throws Exception
   {
      sqlHandler.update("Company.editCompanyData", companyData);
   }
   
   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#updActiveStatus(
    *    org.shs.company.model.CompanyModel)
    */
   public void updActiveStatus(DataHolder companyActvData) throws Exception
   {
      sqlHandler.update("Company.updCompanyActiveStatus", companyActvData);
   }

   // ==========================================================================
   // Following properties relate to worker-company association.
   // ==========================================================================

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#getWorkersForCompany(
    *    org.shs.company.model.WorkerModel)
    */
   @SuppressWarnings("unchecked")
   public ArrayList<WorkerModel> getWorkersForCompany(WorkerModel companyData)
            throws Exception
   {
      return (ArrayList<WorkerModel>) sqlHandler.queryForList(
                           "Worker.getWorkersForCompany", companyData);
   }

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#getCompaniesForWorker(java.lang.String)
    */
   @SuppressWarnings("unchecked")
   public ArrayList<CompanyModel> getCompaniesForWorker(String workerId)
            throws Exception
   {
      return (ArrayList<CompanyModel>) sqlHandler.queryForList(
                              "Worker.getCompaniesForWorker", workerId);
   }
   
   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#addNewCompanyWorkerAssoc(
    *    org.shs.company.model.WorkerModel)
    */
   public String addNewCompanyWorkerAssoc(WorkerModel workerData)
            throws Exception
   {
      return (String) sqlHandler.insert("Worker.addNewCompanyWorkerAssoc", workerData);
   }

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#delCompanyWorkerAssoc(java.lang.String)
    */
   public void delCompanyWorkerAssoc(String workerCompanyAssocId)
            throws Exception
   {
      sqlHandler.delete("Worker.deleteCompanyWorkerAssoc", workerCompanyAssocId);
   }

   /* (non-Javadoc)
    * @see org.shs.company.dao.ICompanyDAO#delAllAssocForWorker(java.lang.String)
    */
   public void delAllAssocForWorker(String workerId) throws Exception
   {
      sqlHandler.delete("Worker.deleteAllAssocForWorker", workerId);
   }

}